package makabakaquestionnaire.zutsoftware.utils.workWx;

import com.alibaba.fastjson.JSONObject;
import makabakaquestionnaire.zutsoftware.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：wjf
 * @date ：Created in 2022/3/16 19:01
 * @description：调用微信API接口
 * @modified By：
 */
public class WXAPIUtil {

    private final static Logger logger = LoggerFactory.getLogger(WXAPIUtil.class);

    /**
     * 获取access token
     *
     * @param key 要获取token的类型
     * @returnaccess_token
     */
    public static Map<String, Object> getAccessToken(String key) {
        String secret = "";
        if(key == "Login_SECRET"){
            secret = WorkWXAPI.Login_SECRET;
        }else {
            secret = WorkWXAPI.SECRET;
        }
        String json = HttpClientUtil.get(String.format(WorkWXAPI.GET_ACCESS_TOKEN_URL, WorkWXAPI.CORPID, secret));
        Map<String, Object> data = JsonUtil.WXJsonToMap(json);
        if (Integer.parseInt(data.get("errcode").toString()) == 0) {
            return data;
        } else {
            logger.info("{} get access error,msg:{}", key, data.toString());
        }
        return null;
    }

}
